package tests.utils;

import junit.framework.Assert;

import org.junit.Test;

import utils.math.MathCodes;

public class HornerTest {

    private final double[][] polynoms = { { 1, 2, 3, 4, 5 }, { 5, 5, 5, 5, 5 } };

    @Test
    public void algorithmCorrectnessTest() {

	int polynomsCount = polynoms.length;
	int x = 5;

	for (int k = 0; k < polynomsCount; k++) {
	    Assert.assertTrue(MathCodes.horner(polynoms[k], x) == calculatePolyAtPoint(
		    polynoms[k], x));
	}

    }

    private double calculatePolyAtPoint(double[] polyCoeff, double x) {
	double answer = 0.0;
	int arrayLength = polyCoeff.length;

	for (int k = 0; k < arrayLength; k++) {
	    answer += Math.pow(x, k) * polyCoeff[k];
	}

	return answer;
    }

}
